var c = require('../lib/common.js');
var cf = require('../lib/config.js');
Page({
  data: {
    riderinfo: '正在路上...',
    rider: 1,
    dis: '暂无数据',
    markers: [{
      iconPath: "/images/myaddress.png",
      id: 0,
      latitude: '',
      longitude: '',
      width: 30,
      height: 30,

    },
    {
      iconPath: "/images/motuo.png",
      id: 1,
      latitude: '',
      longitude: '',
      width: 30,
      height: 30,
      callout: {
        content: '没有服务对象',
        fontSize: 12,
        padding: 10,
        borderRadius: 10,
        color: '#ffffff',
        bgColor: '#444444',
        display: "ALWAYS"
      },
    }
    ],
    polyline: [{
      points: [{
        longitude: '',
        latitude: ''
      }, {
        longitude: '',
        latitude: ''
      }],
      color: "#000000",
      width: 2,
      dottedLine: true
    }],
    controls: [{
      id: 1,
      iconPath: '/images/1.jpg',
      position: {
        left: 0,
        top: 300 - 50,
        width: 50,
        height: 50
      },
      clickable: true
    }]
  },

  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },
  onLoad: function (option) {
    var addressid = option.addressid ? option.addressid : option;
    console.log(addressid);
    var that = this;
    //用户本身的经纬度
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        //获取address地理位置
        c.ajax(
          cf.hostapi + 'toIdGetAddressInfo', {
            id: addressid,
          },
          res => {
            console.log('res');
            if (res.data.retcode == 200) { //获取到了地址的经纬度
              var phone = res.data.data.phone;
              //骑手经纬度用来 计算距离
              var lat_rid = latitude;
              var lng_rid = longitude;
              //地址的经纬度  用来计算距离
              var lat_address = res.data.data.lat;
              var lng_address = res.data.data.lng;
              //通过上边的经纬度来计算距离
              that.distance(lat_rid, lng_rid, lat_address, lng_address);
              //设置地图上显示的两个点的 经纬度
              var rid_lat = 'polyline[0].points[1].latitude';
              var rid_lng = 'polyline[0].points[1].longitude';
              var address_lat = 'polyline[0].points[0].latitude';
              var address_lng = 'polyline[0].points[0].longitude';
              //设置骑手经纬度图标
              var mark_rid_w   = 'markers[1].width';
              var mark_rid_h   = 'markers[1].height';
              var mark_rid_ico = 'markers[1].iconPath';
              var mark_rid_lat = 'markers[1].latitude';
              var mark_rid_lng = 'markers[1].longitude';
              //设置地址经纬度图标
              var mark_address_w = 'markers[0].width';
              var mark_address_h = 'markers[0].height';
              var mark_address_ico = 'markers[0].iconPath';
              var mark_address_lat = 'markers[0].latitude';
              var mark_address_lng = 'markers[0].longitude';
              //设置骑手气泡
              var mark_address_content = 'markers[1].callout.content';
              var mark_address_padding = 'markers[1].callout.padding';
              var mark_address_borderRadius = 'markers[1].callout.borderRadius';
              var mark_address_color = 'markers[1].callout.color';
              var mark_address_bgColor = 'markers[1].callout.bgColor';
              var mark_address_display = 'markers[1].callout.display';
              var mark_address_fontSize = 'markers[1].callout.fontSize';
              that.setData({
                phone: phone,
                [mark_address_w]: 30,
                [mark_address_h]: 30,
                [mark_address_ico]: '/images/myaddress.png',
                [mark_address_content]: that.data.dis,
                [mark_address_padding]: 10,
                [mark_address_borderRadius]: 10,
                [mark_address_color]: '#ffffff',
                [mark_address_bgColor]: '#444444',
                [mark_address_display]: 'ALWAYS',
                [mark_address_fontSize]: 12,
                [mark_rid_w]: 30,
                [mark_rid_h]: 30,
                [mark_rid_ico]: '/images/motuo.png',
                //骑手的经纬度赋值
                [rid_lat]: lat_rid,
                [rid_lng]: lng_rid,
                //地址经纬度赋值
                [address_lat]: lat_address,
                [address_lng]: lng_address,
                //骑手图标
                [mark_rid_lat]: lat_rid,
                [mark_rid_lng]: lng_rid,
                //地址图标
                [mark_address_lat]: lat_address,
                [mark_address_lng]: lng_address,
                //地图中心经纬度
                longitude: longitude,
                latitude: latitude,
              });
              setTimeout(function () {
                that.onLoad(addressid)
              }, 10000)
            } else {//no rider 
              console.log('没有获取到了骑手信息');
              that.setData({ riderinfo: '暂无进行的订单', rider: 0 })
            }
          }

        )


      }
    })




    // var address_id_user = 10;
    // var address_id_moto = 11;
    // var that = this;
    // c.ajax(
    //   cf.hostapi + 'toIdGetAddressInfo', {
    //     id: address_id_user
    //   },
    //   res => {

    //     c.ajax(
    //       cf.hostapi + 'toIdGetAddressInfo', {
    //         id: address_id_moto
    //       },
    //       res => {


    //         // console.log(that.data);
    //       }
    //     )

    //   }
    // )




  },
  distance: function (la1, lo1, la2, lo2) {
    var content = 'markers.callout.content';
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = Math.round(s * 10000) / 10000;
    console.log("计算结果", s)
    if (s < 1) {
      this.setData({
        dis: ('距离' + parseInt(s * 1000) + 'm')
      })
    } else {
      this.setData({
        dis: ('距离' + parseInt(s) + 'km')
      })
    }

    // console.log(that.data.polyline[0].points[0].longitude);
    // var pol ="polyline[0].points[0].longitude";
    // that.setData({ [pol]: 113.3245211 })
    // setTimeout(function(){
    //   that.setData({ [pol]: 113.3245211 })
    // },2000);
  },
  click: function () {
    var that = this;
    // console.log(that.data.markers[0].longitude[0].longitude);
    console.log(this.data);
    var pol = 'polyline[0].points[0].longitude';
    var rel = 'markers[0].longitude';
    that.setData({
      [pol]: 113.3845211,
      mi: 100,
      [rel]: 113.3845211,
      longitude: 113.3845211
    });
  },
  kefuclick: function () {
    var phone = this.data.phone;
    wx.showModal({
      title: '',
      content: phone,
      confirmText: '拨打',
      success(res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: phone,
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})